package com.zhongkai.web.control.book;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.book.TXtXxts;
import com.zhongkai.model.config.User;
import com.zhongkai.service.CommonService;
import com.zhongkai.service.book.ReadMessageService;
import com.zhongkai.tools.CheckWord;

@Controller
//system/book/read_message.do
//system/book/send_message.do
public class ReadMessageControl {
	private ReadMessageService readMessageService ;
	
	@RequestMapping("/system/message/read_message.do")
	public String readMessage(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String message_xh=request.getParameter("xh");
		Integer xh=Integer.valueOf(message_xh);
		TXtXxts tXtXxts=(TXtXxts) readMessageService.findById(new TXtXxts(), xh);
		tXtXxts.setZt("Y");		
		readMessageService.update(tXtXxts);				
		return "redirect:/system/message/message_read_form.jsp?xh="+xh;		
	}
	
	@RequestMapping("/system/message/send_message.do")
	public String sendMessage(String acceptPerson,TXtXxts tXtXxts,HttpServletRequest request,HttpServletResponse response) throws Exception{
		PrintWriter out=response.getWriter();
		User send_user=(User) request.getSession().getAttribute("user");
		User accept_user=(User) readMessageService.findByName("User",new String[]{"userAccount"}, new String[]{acceptPerson});
		if(accept_user!=null){
			if(tXtXxts.getXxbt()==null||"".equals(tXtXxts.getXxbt())){
				out.write(CheckWord.showMsg("标题不能为空!", "/system/message/message_add_form.jsp?acceptPerson="+acceptPerson,request));
				return null;
			}
			if(tXtXxts.getXxnr()==null||"".equals(tXtXxts.getXxnr())){
				out.write(CheckWord.showMsg("回复内容不能为空!", "/system/message/message_add_form.jsp?acceptPerson="+acceptPerson,request));
				return null;
			}
			String send_userDm=send_user.getUserIdentifier();
			String accept_userDm=accept_user.getUserIdentifier();
			tXtXxts.setFjrDm(send_userDm);
			tXtXxts.setSjrDm(accept_userDm);
			tXtXxts.setZt("N");
			readMessageService.save(tXtXxts);
			out.write(CheckWord.showMsg("发送成功,返回信息列表!", "/system/message/message_list_form.jsp",request));
			return null;
		}else{
			out.write(CheckWord.showMsg("收件人不存在!", "/system/message/message_add_form.jsp?acceptPerson="+acceptPerson,request));
			return null;
		}
	}
	
	
	
	
	
	public ReadMessageService getReadMessageService() {
		return readMessageService;
	}

	@Resource
	public void setReadMessageService(ReadMessageService readMessageService) {
		this.readMessageService = readMessageService;
	}
	
	
	
	
	

}
